using Agron.Data.Contracts;
using Agron.Services.Contracts;

using System;
using System.Collections.Generic;
using System.ServiceModel;


namespace Agron.Services.Behaviors
{
    /// <summary>
    /// Implements the <see cref="ISchedulerService"/> service.
    /// </summary>
    [ServiceBehavior]
    public class SchedulerService : ISchedulerService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SchedulerService"/> class.
        /// </summary>
        public SchedulerService() { }

        #region ISessionManager Members

        /// <summary>
        /// Creates a new session record in the data store.
        /// </summary>
        /// <param name="session">The <see cref="Session"/> data contract that will be created in the data store.</param>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception>
        [OperationBehavior]
        public void CreateSession(Session session)
        {
            if (null == session)
                throw new ArgumentNullException("session");

            DateTime startUtc = session.Start.ToUniversalTime();
            DateTime finishUtc = session.Finish.ToUniversalTime();
           
            //Database database = DatabaseFactory.CreateDatabase("Agron");
            //using (DbCommand command = database.GetStoredProcCommand("agron_Session_CreateSession"))
            //{
            //    database.AddInParameter(command, "Owner", DbType.Guid, session.OwnerId);
            //    database.AddInParameter(command, "Subject", DbType.String, session.Subject);
            //    database.AddInParameter(command, "Description", DbType.String, session.Description);
            //    database.AddInParameter(command, "Start", DbType.DateTime, startUtc);
            //    database.AddInParameter(command, "Duration", DbType.Double, finishUtc.Subtract(startUtc).TotalMinutes);
            //    database.AddInParameter(command, "RequiresPassword", DbType.Boolean, session.RequiresPassword);
            //    database.AddInParameter(command, "SessionPassword", DbType.String, session.Password);
                
            //    database.ExecuteNonQuery(command);
            //}
        }

        /// <summary>
        /// Deletes the specified session.
        /// </summary>
        /// <param name="sessionId">An <see cref="Int32"/> that uniquely specifies the session within the data store.</param>
        [OperationBehavior]
        public void DeleteSession(int sessionId)
        {
            //Database database = DatabaseFactory.CreateDatabase("Agron");
            //using (DbCommand command = database.GetStoredProcCommand("agron_Session_DeleteSession"))
            //{
            //    database.AddInParameter(command, "SessionId", DbType.Int32, sessionId);
            //    database.ExecuteNonQuery(command);
            //}
        }

        /// <summary>
        /// Updates an existing session in the data store.
        /// </summary>
        /// <param name="session">The <see cref="Session"/> data contract that will be updated in the data store.</param>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception>
        [OperationBehavior]
        public void UpdateSession(Session session)
        {
            if (null == session)
                throw new ArgumentNullException("session");

            DateTime startUtc = session.Start.ToUniversalTime();
            DateTime finishUtc = session.Finish.ToUniversalTime();

            //Database database = DatabaseFactory.CreateDatabase("Agron");
            //using (DbCommand command = database.GetStoredProcCommand("agron_Session_UpdateSession"))
            //{
            //    database.AddInParameter(command, "SessionId", DbType.Int32, session.SessionId);
            //    database.AddInParameter(command, "Owner", DbType.Guid, session.OwnerId);
            //    database.AddInParameter(command, "Subject", DbType.String, session.Subject);
            //    database.AddInParameter(command, "Description", DbType.String, session.Description);
            //    database.AddInParameter(command, "Start", DbType.DateTime, startUtc);
            //    database.AddInParameter(command, "Duration", DbType.Double, finishUtc.Subtract(startUtc).TotalMinutes);
            //    database.AddInParameter(command, "RequiresPassword", DbType.Boolean, session.RequiresPassword);
            //    database.AddInParameter(command, "SessionPassword", DbType.String, session.Password);
            //    database.ExecuteNonQuery(command);
            //}
        }

        /// <summary>
        /// Gets a <see cref="IEnumerable{Session}"/> collection of session objects owned by a specified user within a particular range.
        /// </summary>
        /// <param name="userId">A <see cref="Guid"/> that uniquely identifies the user within the datastore.</param>
        /// <param name="min">A <see cref="DateTime"/> value that specifies the minimum part of the date range.</param>
        /// <param name="max">A <see cref="DateTime"/> value that specifies the maximum part of the date range.</param>
        /// <returns>
        /// A <see cref="IEnumerable{Session}"/> collection of session objects owned by a specified user within a particular range.
        /// </returns>
        [OperationBehavior]
        public IEnumerable<Session> GetRange(Guid userId, DateTime min, DateTime max)
        {
            //Database database = DatabaseFactory.CreateDatabase("Agron");
            //using (DbCommand command = database.GetStoredProcCommand("agron_Session_GetRange"))
            //{
            //    database.AddInParameter(command, "UserId", DbType.Guid, userId);
            //    database.AddInParameter(command, "MinRange", DbType.DateTime, min.ToUniversalTime());
            //    database.AddInParameter(command, "MaxRange", DbType.DateTime, max.ToUniversalTime());

            //    IDataReader dataReader = database.ExecuteReader(command);
            //    return SessionDataReaderToEnumerableCollection(dataReader);
            //}
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a <see cref="Session"/> obeject from the datastore.
        /// </summary>
        /// <param name="sessionId">An <see cref="Int32"/> that uniquely specifies the session within the data store.</param>
        /// <returns>
        /// A <see cref="Session"/> obeject from the datastore or <c>null</c> if the session ID is invalid.
        /// </returns>
        [OperationBehavior]
        public Session GetSessionInfo(int sessionId)
        {
            //IEnumerable<Session> collection;

            //Database database = DatabaseFactory.CreateDatabase("Agron");
            //using (DbCommand command  = database.GetStoredProcCommand("agron_Session_GetBySessionId"))
            //{
            //    database.AddInParameter(command, "SessionId", DbType.Int32, sessionId);

            //    IDataReader dataReader = database.ExecuteReader(command);
            //    collection = SessionDataReaderToEnumerableCollection(dataReader);
            //}

            //if (null != collection)
            //{
            //    List<Session> dataList = new List<Session>(collection);
            //    if (1 <= dataList.Count)
            //        return dataList[0];
            //}
            
            return null;
        }

        #endregion

        //private static IEnumerable<Session> SessionDataReaderToEnumerableCollection(IDataReader dataReader)
        //{
        //    List<Session> list = new List<Session>();

        //    if (null != dataReader)
        //    {
        //        while (dataReader.Read())
        //        {
        //            Session session = new Session();
        //            session.SessionId = dataReader.GetInt32(0);
        //            session.OwnerId = dataReader.GetGuid(1);
        //            session.Subject = dataReader.GetString(2);
        //            session.Description = dataReader.IsDBNull(3) ? null : dataReader.GetString(3);
        //            session.Start = dataReader.GetDateTime(4).ToLocalTime();
        //            session.Finish = dataReader.GetDateTime(4).ToLocalTime().AddMinutes(dataReader.GetDouble(5));
        //            session.RequiresPassword = dataReader.GetBoolean(6);
        //            session.Password = dataReader.IsDBNull(7) ? null : dataReader.GetString(7);

        //            list.Add(session);
        //        }
        //    }

        //    return list;
        //}
    }
}
